﻿using System.Collections.Generic;
using System.IO;
using System.Net.Mime;
using Baik.Interface.Execution;
using Baik.Interface.Expression;
using Baik.Interface.Framework;
using IUndefined = Baik.Interface.Execution.IUndefined;

namespace Baik.Execution
{
    public class ExecEnvironment : IExecutionEnvironment
    {
        public ExecEnvironment(IInjector inject, IExpressionParser parser)
        {
            Parser = parser;
            Inject = inject;
            GlobalScope = inject.Instantiate<Baik.Interface.Execution.IObject>();
            //Refresh();
        }
        public IExpressionParser Parser { get; private set; }
        public void Refresh()
        {
            GlobalScope.Assign("system", Inject.Instantiate<ISystem>());
        }
        public IInjector Inject { get; private set; }
        public IScope GlobalScope { get; private set; }
        public IEnumerable<string> Eval(IEnumerable<IExpression> exps)
        {
            throw new global::System.NotImplementedException();
        }
        
        public IObject Load(string filepath)
        {
            return ! File.Exists(filepath) ? Inject.Instantiate<IUndefined>() : Eval(File.ReadAllText(filepath));
        }

        public IObject Eval(string code)
        {
            return Eval(Parser.Parse(code));
        }
        public IObject Eval(IExpression exp)
        {
            return exp.Evaluate(GlobalScope);
          
        }

        public IExpression Parse(string code)
        {
            return Parser.Parse(code);
        }
    }
}